package com.yingside.thread.bank;

public class Bank {
    public static void main(String[] args) {
        Bank b = new Bank();
        b.init();
    }

    public class Family implements Runnable{
        //存款
        private int money = 5000;
        //现金
        private int cash = 0;
        //取钱金额
        private int getMoney = 2000;

        Object key = new Object();

        @Override
        public void run() {

            getCash();

//            synchronized (this){
//                System.out.println(Thread.currentThread().getName() + "到银行取钱---" + getMoney);
//
//                int temp = money - getMoney;
//                int cashTemp = cash + getMoney;
//
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//
//                money = temp;
//                cash = cashTemp;
//            }
        }

        public synchronized void getCash(){
            System.out.println(Thread.currentThread().getName() + "到银行取钱---" + getMoney);

            int temp = money - getMoney;
            int cashTemp = cash + getMoney;

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            money = temp;
            cash = cashTemp;
        }

        public void show(){
            System.out.println("现金:" + cash);
            System.out.println("存款:" + money);
        }
    }

    public void init(){
        Family family = new Family();
        Thread t1 = new Thread(family,"丈夫");
        Thread t2 = new Thread(family,"妻子");
        t1.start();
        t2.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("================");
//        husband.show();
//        System.out.println("================");
        family.show();

    }


}
